package com.wbl.tijiancms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wbl.tijiancms.mapper.OrdersMapper;
import com.wbl.tijiancms.pojo.Orders;
import com.wbl.tijiancms.pojo.Overallresult;
import com.wbl.tijiancms.response.ApiRestResponse;
import com.wbl.tijiancms.response.TijianExceptionEnum;
import com.wbl.tijiancms.service.OverallresultService;
import com.wbl.tijiancms.mapper.OverallresultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author yy
* @description 针对表【overallresult】的数据库操作Service实现
* @createDate 2024-09-18 10:09:07
*/
@Service
public class OverallresultServiceImpl extends ServiceImpl<OverallresultMapper, Overallresult>
    implements OverallresultService{

    @Autowired
    OverallresultMapper overallresultMapper;

    @Autowired
    OrdersMapper ordersMapper;

    @Override
    public ApiRestResponse getResultlist(Overallresult overallresult) {
        QueryWrapper<Overallresult> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",overallresult.getOrderid());
        List<Overallresult> overallresults = overallresultMapper.selectList(queryWrapper);

        return ApiRestResponse.success(overallresults);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiRestResponse ConfirmOrder(List<Overallresult> overallresult) throws Exception{
        QueryWrapper<Overallresult> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",overallresult.get(0).getOrderid());
        List<Overallresult> overallresults = overallresultMapper.selectList(queryWrapper);
        List<Overallresult> os =  overallresult.stream().filter(overallresult1 -> !overallresults.contains(overallresult1)).collect(Collectors.toList());
        int i = overallresultMapper.insertList(os);
//        int ii = ordersMapper.updateOrderState(overallresult.get(0).getOrderid(),4);
        if (i>0){
            return ApiRestResponse.success(os);
        }else {
            return ApiRestResponse.error(TijianExceptionEnum.SYSTEM_ERROR);
        }
    }



    @Override
    public ApiRestResponse getResultlistbyUser(Orders order) {
        QueryWrapper<Overallresult> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",order.getOrderid());
        List<Overallresult> overallresults = overallresultMapper.selectList(queryWrapper);
        return ApiRestResponse.success(overallresults);
    }
}




